import React from 'react';
import { connect } from 'dva';
import BasePage from './BasePage'
import CurrentLocation from '../components/currentLocation'
import breadArr from '../constant/breadArr'
import PatentDataTable from '../components/table/patentDataTable'
import { Space, Button, Upload, Modal, Form, Input, Select, Popconfirm, message } from 'antd'
import constant from '../constant/constant'
import PatentDataSearchForm from '../components/patentDataSearchForm'
const { Option } = Select

const applicationTypeArr = ["个体", "企业", "大专院校", "高校", "科研单位", "产学合作", "其他"]
const patentTypeArr = ["发明授权", "实用新型", "外观设计"]

class PatentDataAdministerPage extends React.Component {
    constructor(props) {
        super(props),
            this.state = {
                uploadModal: false,
                deletedIds:[]
            }
    }

    componentDidMount(){
        this.props.dispatch({
            type:"patentData/setDeletedIds",
            data:[]
        })
    }

    showModal = () => {
        this.setState({
            uploadModal: true
        })
    }

    showOffModal = () => {
        this.setState({
            uploadModal: false
        })
    }

    setDeleteIds = ids => {
        this.setState({
            deletedIds:ids
        })
    }

    onChangeUpload = info => {
        if (info.file.status == "error") {
            this.props.dispatch({
                type: "app/setServerRes",
                data: {
                    code: 500,
                    msg: "专利数据文件上传错误"
                }
            })
        }
        if (info.file.status == "done") {
            if (info.file.response.code == 200) {
                this.props.dispatch({
                    type: "patentData/getPatentDataTableList",
                })

                setTimeout(()=>{
                    this.props.dispatch({
                        type: "app/setServerRes",
                        data: info.file.response
                    })
                },800)
            }
        }
    }

    onFinishPatent = values => {
        this.props.dispatch({
            type: "patentData/uploadPatentData",
            data: values
        })

        this.setState({
            uploadModal: false
        })
    }

    deleteSelected = () => {
        if(this.props.deletedIds.length == 0){
            message.error("请先选择需要删除的数据！")
            return
        }
        this.props.dispatch({
            type: "patentData/deletePatentById",
            data: {
                id: this.props.deletedIds
            }
        })
    }

    refreshData = () => {
        window.location.reload()
    }

    render() {
        return (
            <BasePage>
                <CurrentLocation breadArr={breadArr.PatentDataAdministerPage}></CurrentLocation>
                <Space size="large" style={{ marginLeft: 654, marginTop: 10 }}>
                    <Button type="primary" onClick={this.showModal}>单个录入</Button>
                    <Upload
                        accept={".xlsx"}
                        action={constant.serverUrl + "/tvs/api/patents/file/import/exceldata"}
                        onChange={this.onChangeUpload}
                        name={"patentfile"}
                        itemRender={() => { null }}
                    >
                        <Button type="primary">批量导入</Button>
                    </Upload>
                    <Button type="primary"><Popconfirm onConfirm={this.deleteSelected} title={"确定是否删除选中的数据？"}>删除选中</Popconfirm></Button>
                    <Button type="primary" onClick={this.refreshData}>刷新数据</Button>
                </Space>
                <div style={{marginTop:20}}><PatentDataSearchForm></PatentDataSearchForm></div>
                <div style={{ marginTop: 20 }}><PatentDataTable></PatentDataTable></div>
                <Modal
                    title={"单个录入专利数据"}
                    visible={this.state.uploadModal}
                    onCancel={this.showOffModal}
                    footer={null}
                    destroyOnClose={true}
                >
                    <Form
                        name="patent"
                        onFinish={this.onFinishPatent}
                    >
                        <Form.Item
                            label="标题"
                            name="patentTitle"
                            rules={[{ required: true, message: '请输入标题!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="摘要"
                            name="abstract"
                            rules={[{ required: true, message: '请输入摘要!' }]}
                        >
                            <Input.TextArea autoSize={true} maxLength={200} />
                        </Form.Item>

                        <Form.Item
                            label="文献页数"
                            name="patentPages"
                            rules={[{ required: true, message: '请输入文献页数!' }]}
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="公开号"
                            name="publicationNumber"
                            rules={[{ required: true, message: '请输入公开号!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="公开日"
                            name="publlcationDate"
                            rules={[{ required: true, message: '请输入公开日!' }]}
                        >
                            <Input type={"date"} />
                        </Form.Item>

                        <Form.Item
                            label="申请号"
                            name="applicationNumber"
                            rules={[{ required: true, message: '请输入申请号!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="申请日"
                            name="applicationDate"
                            rules={[{ required: true, message: '请输入公开日!' }]}
                        >
                            <Input type={"date"} />
                        </Form.Item>

                        <Form.Item
                            label="申请人"
                            name="applicant"
                            rules={[{ required: true, message: '请输入申请人!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="申请人省市（代码）"
                            name="applicantProvince"
                            rules={[{ required: true, message: '请输入申请人省市!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="申请人类型"
                            name="applicantType"
                            rules={[{ required: true, message: '请输入申请人类型!' }]}
                        >
                            <Select style={{ width: 200 }}>
                                {applicationTypeArr.map((item, index) => {
                                    return (
                                        <Option value={item}>{item}</Option>
                                    )
                                })}
                            </Select>
                        </Form.Item>

                        <Form.Item
                            label="权利要求数量"
                            name="claimNumber"
                            rules={[{ required: true, message: '请输入权利要求数量!' }]}
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="首次公开日"
                            name="firstOpenDay"
                        >
                            <Input type={"date"} />
                        </Form.Item>

                        <Form.Item
                            label="授权公开日"
                            name="privilegedTime"
                            rules={[{ required: true, message: '请输入授权公开日!' }]}
                        >
                            <Input type={"date"} />
                        </Form.Item>

                        <Form.Item
                            label="专利类型"
                            name="patentType"
                            rules={[{ required: true, message: '请输入专利类型!' }]}
                        >
                            <Select style={{ width: 200 }}>
                                {patentTypeArr.map((item, index) => {
                                    return (
                                        <Option value={item}>{item}</Option>
                                    )
                                })}
                            </Select>
                        </Form.Item>

                        <Form.Item
                            label="发明人"
                            name="inventor"
                            rules={[{ required: true, message: '请输入发明人!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="发明人数量"
                            name="inventorNumber"
                            rules={[{ required: true, message: '请输入发明人数量!' }]}
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="主分类号"
                            name="mainClassification"
                            rules={[{ required: true, message: '请输入主分类号!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="IPC"
                            name="ipc"
                            rules={[{ required: true, message: '请输入IPC!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="IPC分类号数量"
                            name="ipcNumber"
                            rules={[{ required: true, message: '请输入IPC分类号数量!' }]}
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="简单同族个数"
                            name="patentFamily"
                            rules={[{ required: true, message: '请输入简单同族个数!' }]}
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="扩展同族个数"
                            name="extendedFamily"
                            rules={[{ required: true, message: '请输入扩展同族个数!' }]}
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="inpadoc同族个数"
                            name="inpadocFamily"
                            rules={[{ required: true, message: '请输入inpadoc同族个数!' }]}
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="引证专利"
                            name="patentCitation"
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="引证专利数量"
                            name="patentCitationNumber"
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="被引证次数"
                            name="forwardCitationNumber"
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="引证科技文献"
                            name="literatureCitation"
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="引证科技文献数"
                            name="literatureCitationNumber"
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="专利寿命（月）"
                            name="patentLife"
                        >
                            <Input type={"number"} />
                        </Form.Item>

                        <Form.Item
                            label="转让执行日"
                            name="transferDate"
                        >
                            <Input type={"date"} />
                        </Form.Item>

                        <Form.Item
                            label="被引证国别"
                            name="forwardCitationCountry"
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="公开国别"
                            name="publlcationCountry"
                            rules={[{ required: true, message: '请输入公开国别!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item>
                            <Button type="primary" htmlType="submit">
                                提交
                            </Button>
                        </Form.Item>
                    </Form>
                </Modal>
            </BasePage>
        )
    }
}

const mapStateToProps = state => {
    let deletedIds = state.patentData.selectedIds
    return {
        deletedIds
    }
}

export default connect(mapStateToProps)(PatentDataAdministerPage)